package com.sskj.gocoin.v.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.adapter.MyFragmentPagerAdapter;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.rxbus2.RxBus;
import com.sskj.gocoin.common.rxbus2.Subscribe;
import com.sskj.gocoin.common.rxbus2.ThreadMode;
import com.sskj.gocoin.m.bean.rxbus.TabChange;
import com.sskj.gocoin.p.fragment.FabiFragmentPresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.BindEmailActivity;
import com.sskj.gocoin.v.activity.FabiDetailActivity;
import com.sskj.gocoin.v.activity.FabiPublishActivity;
import com.sskj.gocoin.v.activity.LoginActivity;
import com.sskj.gocoin.v.activity.MainActivity;
import com.sskj.gocoin.v.activity.SellerApplyActivity;
import com.sskj.gocoin.v.base.BaseFragment;

import java.util.ArrayList;

import butterknife.BindView;

public class FabiFragment extends BaseFragment<MainActivity, FabiFragmentPresenter> {

    @BindView(R.id.iv_right_end)
    ImageView ivRightEnd;  // 右侧图标  发布出售-发布购买
    //    @BindView(R.id.iv_right_start)  // 第一个图标  从左侧开始看
//            ImageView ivRightStart;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.tv_sell)
    TextView tvSell;
    @BindView(R.id.tv_record)
    TextView tvRecord;
    @BindView(R.id.view_left)
    View viewLeft;
    @BindView(R.id.view_middle)
    View viewMiddle;
    @BindView(R.id.view_right)
    View viewRight;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private AlertDialog tipDialog;
    private AlertDialog cancleTipDialog;

    public static FabiFragment newInstance() {
        Bundle args = new Bundle();
        FabiFragment fragment = new FabiFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_fabi;
    }

    @Override
    public FabiFragmentPresenter getPresenter() {
        return new FabiFragmentPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(FabiBuyAndSellFragment.newInstance(false));
        fragments.add(FabiBuyAndSellFragment.newInstance(true));
        fragments.add(FabiRecordFragment.newInstance());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                changeTextColor(position);
            }
        });
        ClickUtil.click(tvBuy, () -> {
            changeTab(new TabChange(false));
        });
        ClickUtil.click(tvSell, () -> {
            changeTab(new TabChange(true));
        });
        ClickUtil.click(tvRecord, () -> {
            changeTab(null);
        });
//        ClickUtil.click(ivRightStart, () -> {
//            if (MyAppLication.isLogin()) {
//                FabiDetailActivity.start(getActivity());
//            } else {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiFragment200));
//                LoginActivity.start(getActivity());
//            }
//        });
        ClickUtil.click(ivRightEnd, () -> {
            FabiPublishActivity.start(mActivity);

//            if (MyAppLication.isLogin()) {
//                if (checkIdent(getActivity())) {
//                    if (mActivity.checkPwdStatus()) {
//                        if (MyAppLication.isShop()) {
//                            FabiPublishActivity.start(mActivity);
//                        } else {
//                            showTip();
//                        }
//                    }
//                }
//            } else {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.strFabiFragment200));
//                LoginActivity.start(getActivity());
//            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeTab(TabChange tabChange) {
        int i;
        if (tabChange == null) {
            i = 2;
        } else {
            i = tabChange.isBuy() ? 1 : 0;
        }
        changeTextColor(i);
        if (viewPager.getCurrentItem() != i) {
            viewPager.setCurrentItem(i, true);
        }
    }

    /**
     * 更改选项卡
     *
     * @param position
     */
    private void changeTextColor(int position) {
        viewLeft.setVisibility(position == 0 ? View.VISIBLE : View.INVISIBLE);
        viewMiddle.setVisibility(position == 1 ? View.VISIBLE : View.INVISIBLE);
        viewRight.setVisibility(position == 2 ? View.VISIBLE : View.INVISIBLE);
        tvBuy.setTextColor(getResources().getColor(position == 0 ? R.color.white : R.color.fabi_item_text_color));
        tvSell.setTextColor(getResources().getColor(position == 1 ? R.color.white : R.color.fabi_item_text_color));
        tvRecord.setTextColor(getResources().getColor(position == 2 ? R.color.white : R.color.fabi_item_text_color));

    }

    /**
     * 显示申请商家的提示
     */
    private void showTip() {
        if (tipDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            View view = View.inflate(getActivity(), R.layout.dialog_fabi_tip, null);
            ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
                tipDialog.dismiss();
            });
            ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
                tipDialog.dismiss();
                SellerApplyActivity.start(getActivity());
            });
            builder.setView(view);
            builder.setCancelable(true);
            tipDialog = builder.create();
        }

        tipDialog.show();
    }

    /**
     * 显示解除商家认证
     */
    private void showCancleTip() {
        if (cancleTipDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            View view = View.inflate(getActivity(), R.layout.dialog_fabi_cancle_seller_tip, null);
            ClickUtil.click(view.findViewById(R.id.tv_cancel), () -> {
                cancleTipDialog.dismiss();
            });
            ClickUtil.click(view.findViewById(R.id.tv_apply), () -> {
                cancleTipDialog.dismiss();
                SellerApplyActivity.start(getActivity());
            });
            builder.setView(view);
            builder.setCancelable(true);
            cancleTipDialog = builder.create();
        }

        cancleTipDialog.show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }
}
